<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-05-14 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-20 09:53:35
 */

namespace app\weixin_manage\controller;
use app\common\controller\Backend;

class Firstpush extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '关注推送', '公众号管理', 'firstpush');

		$this->weixinFirsttextDao = model('weixin_manage/WeixinFirsttext');
		$this->weixinFirstpushDao = model('weixin_manage/WeixinFirstpush');
	}

	function index() {
		if (!$this->wid) {
			$this->adminError('微信wid有误');
		}
		//获取关注推送文字
		$info = $this->weixinFirsttextDao->getInfoByWid($this->wid);
		//print_r($info);
		$this->assign('id', $info['id']);
		$this->assign('info', $info);

		//关注推送类型
		$firstpush_type_config = array(
			'1' => '文字',
			'2' => '图片',
			'3' => '图文列表',
		);
		$this->assign('firstpush_type_config', $firstpush_type_config);

		//获取关注推送图文列表
		list($list) = $this->weixinFirstpushDao->getListByWid($this->wid);
		$this->assign('list', $list);

		return $this->fetch();
	}

	public function doTextAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		$info = $this->weixinFirsttextDao->get($id);

		//普通上传图片
		list($uploadInfo, $isDelOldImg) = $this->comuploadFile('weixin/firsttext', 'picurl', $info['picurl'], 0);
		$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];

		//将图片上传到微信后台
		if ($uploadInfo) {
			$filename = $uploadInfo['savepath'].$uploadInfo['savename'];

			//先绑定微信公众号
			$weixinAccountDao = model('WeixinAccount');
			$weixinAccountInfo = $weixinAccountDao->getInfo($this->wid);
			//print_r($weixinAccountInfo);exit;

			$options = array(
				'token'=>$weixinAccountInfo['token'], //填写你设定的key
				'encodingaeskey'=>$weixinAccountInfo['aeskey'], //填写加密用的EncodingAESKey
				'appid'=>$weixinAccountInfo['appid'], //填写高级调用功能的app id, 请在微信开发模式后台查询
				'appsecret'=>$weixinAccountInfo['appsecret'], //填写高级调用功能的密钥
				'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
				'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
				'paysignkey'=>'' //商户签名密钥Key，支付权限专用
			);
			$weObj = new \Common\Lib\Util\wechat($options);

			$dataWeixin = array(
				'media' => '@'.$filename,
			);
			//print_r($data);
			$result = $weObj->uploadMedia($dataWeixin, 'image');

			//{"type":"image","media_id":"bohSnA_sUfyNNkehT96NNAsYRJJpv4ZDBGvFn7hkuJLEP3JL8CzsS-S20q9yL0jO","created_at":1531215297}
			if ($result !== false) {
				$data['media_id'] = $result['media_id'];

				//删除原图
				unlink($filename);
			} else {
				$this->error("生成菜单失败，请到公众平台里面生成，错误提示：".$weObj->errMsg);
			}
		}

		if ($id) {
			$result = $this->weixinFirsttextDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功');
			} else {
				$this->error('保存失败');
			}
		} else {
			$data['wid'] = $this->wid;
			$data['create_time'] = $this->nowtime;
			$result = $this->weixinFirsttextDao->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
		}
	}

	public function listorders() {
		$result = parent::listorders($this->weixinFirstpushDao);

		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->weixinFirstpushDao->getInfo($id);
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		//处理编辑器内容
		$data['link'] = ueditorContent($data['link']);
		
		//普通上传图片
		list($uploadInfo, $isDelOldImg) = $this->comuploadFile('weixin/firstpush', 'picurl', $info['picurl']);
		$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];

		//普通上传图片
		list($uploadInfo, $isDelOldImg) = $this->comuploadFile('weixin/firstpush', 'picurl', $info['picurl']);
		$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];

		if ($id) {
			$result = $this->weixinFirstpushDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功', url('weixin_manage/firstpush/index', array('wid'=>$this->wid)));
			} else {
				$this->error('保存失败');
			}
		} else {
			$data['wid'] = $this->wid;
			$data['create_time'] = $this->nowtime;
			$result = $this->weixinFirstpushDao->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功', url('weixin_manage/firstpush/index', array('wid'=>$this->wid)));
			} else {
				$this->error('添加失败');
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');
		if (!$id) {
			$this->error('非法ID');
		}

		//获取信息
		$firstpushInfo = $this->weixinFirstpushDao->get($id);

		//删除对应数据库信息
		$result = $this->weixinFirstpushDao->delInfo($id);

		//删除图片文件
		$firstpushInfo['picurl'] && $this->delFtpFile($firstpushInfo['picurl']);

		if ($result !== false) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败'.showDbError($this->weixinFirstpushDao));
		}
	}

}